# -*- coding: utf-8 -*-
import tornado.ioloop
import tornado.web
import threading
import os
from tool import que_end
from task import do_end, do_client
from cfg import g_client_count


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("send\n\r")


def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])


def doEnd(pre_que):
    do_end(pre_que)


def doClient(wid):
    do_client(wid)


if __name__ == "__main__":
    if not os.path.exists("./log/pic"):
        os.makedirs("./log/pic")

    for i in range(0, g_client_count):
        t = threading.Thread(target=doClient, args=(i,))
        t.daemon = True
        t.start()

    t_e = threading.Thread(target=doEnd, args=(que_end,))
    t_e.daemon = True
    t_e.start()

    app = make_app()
    app.listen(8999, "127.0.0.1")

    tornado.ioloop.IOLoop.current().start()
